/**
 * 表单设计器----选择菜单
 */
mini.parse();
var tree = mini.get("tree");
var grid;
var cols;
tree.loading();

//确定按钮之前 here
function onTableOk() {

//    var nodes = tree1.getList();
    var nodes = tree.getData();
    console.log(nodes);

    var group_cols = [];
    for(var i=0;i<nodes.length;i++) {
        if(tree.isLeaf(nodes[i])) {//第一层是叶子节点
            group_cols.push({
                headerAlign: "center", align: "center",
                header: nodes[i].header,
                field: nodes[i].field,
                name: nodes[i].name,
                editor: nodes[i].editor,
            });
        }else {
            group_cols.push({//分组
                headerAlign: "center", align: "center",
                header: nodes[i].text,
                name: 'header-1',
                columns:[]
            });
            var nodes2 = nodes[i].children;
            for(var j=0;j<nodes2.length;j++) {
                console.log(tree.isLeaf(nodes2[j]),'第二层');
                if (tree.isLeaf(nodes2[j])) {//第二层为叶子节点
                    group_cols[group_cols.length - 1].columns.push({
                    	headerAlign: "center", align: "center", 
                        header: nodes2[j].header,
                        field: nodes2[j].field,
                        name: nodes2[j].name,
                        editor: nodes2[j].editor
                    });
                }else {
                    group_cols[group_cols.length - 1].columns.push({//分组
                        headerAlign: "center", align: "center",
                        header: nodes2[j].text,
                        name: 'header-2',
                        columns:[]
                    });
                    var nodes3 = nodes2[j].children;
                    for(var k=0;k<nodes3.length;k++) {
                        console.log(tree.isLeaf(nodes3[k]),'第3层');
                        if (tree.isLeaf(nodes3[k])) {//第三层为叶子节点
                            group_cols[group_cols.length - 1].columns[group_cols[group_cols.length - 1].columns.length - 1].columns.push({
                            	headerAlign: "center", align: "center", 
                            	header: nodes3[k].header,
                                field: nodes3[k].field,
                                name: nodes3[k].name,
                                editor: nodes3[k].editor
                            });
                        }else {
                            // mini.alert("分组最多三级，请重新设置！");
                            // return;
                        }
                    }
                }
            }
        }
    }
    console.log(group_cols,'group_cols');
    return group_cols; //返回选中的数据给父组件
}

function SetData(data) {

    var cols = data.cols;
    console.log(cols)
    var tree_data = [];
    for(var i=0;i<cols.length;i++) {

        tree_data.push({
            text: cols[i].header,
            pid: "-1",
            id: cols[i].name,
            editor:cols[i].editor,
            name:cols[i].name,
            header:cols[i].header,
            field:cols[i].field
        });

        if(cols[i].columns != undefined) {//二层

            var cols2 = cols[i].columns;
            for(var j=0;j<cols2.length;j++) {

                tree_data.push({
                    text: cols2[j].header,
                    pid: cols[i].name,
                    id: cols2[j].header,
                    editor:cols2[j].editor,
                    name:cols2[j].name,
                    header:cols2[j].header,
                    field:cols2[j].field
                });

                if(cols2[j].columns != undefined) {//三层

                    var cols3 = cols2[j].columns;
                    for(var k=0;k<cols3.length;k++) {
                        tree_data.push({
                            text: cols3[k].header,
                            pid: cols2[j].header,
                            id: cols3[k].header,
                            editor:cols3[k].editor,
                            name:cols3[k].name,
                            header:cols3[k].header,
                            field:cols3[k].field
                        });
                    }
                }
            }
        }
    }
    console.log(tree_data);
//    tree.loadData(tree_data);
    tree.loadList(tree_data, "id", "pid");
//    tree.setData([{
//    	text:'cs',id:'1',pid:'-1'
//    },{
//    	text:'1-1',id:'2',pid:'1'
//    }]);
}

//确定按钮
function onOk(e) {
    CloseWindow("ok");
}
//取消按钮
function onCancel(e) {
    CloseWindow("cancel");
}

function CloseWindow(action) {

    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}

function onAddBefore(e) {
    var node = tree.getSelectedNode();

    var newNode = {name:'header-1'};
    tree.addNode(newNode, "before", node);
}
function onAddAfter(e) {
    var node = tree.getSelectedNode();

    var newNode = {name:'header-1'};
    tree.addNode(newNode, "after", node);
}
function onAddNode(e) {
    var node = tree.getSelectedNode();
    
    var newNode = {name:'header-2'};
    if(node.name == 'header-1') {
    	tree.addNode(newNode, "add", node);
    }else {
        if(node.name == 'header-2') {
            mini.alert("分组层数超过限制");
        }else {
            mini.alert("不是分组，无法插入");
        }

    }
    
}
function onEditNode(e) {
    var node = tree.getSelectedNode();

    if(node._state == 'added') {
    	tree.beginEdit(node);
    }else {
    	mini.alert("不是分组，无法编辑");
    }
}
function onRemoveNode(e) {

    var node = tree.getSelectedNode();

    if (node) {
    	if(node.header.indexOf('header') == -1) {
    		mini.alert("不是分组，无法删除");
    	}else if(!isLeaf(node)) {
    		mini.alert("含有子节点，无法删除");
    	}else {
    		 if (confirm("确定删除选中节点?")) {
	            tree.removeNode(node);
	        }
    	}
    }
}

function onSelectionChanged(e) {
	console.log(e);
}

//拖拽投放前发生
tree.on("beforedrop", function (e) {

  if(e.action == 'add') {//加入节点
      if(e.dropNode.name.indexOf('header') == -1) {
          e.cancel = true;
          mini.alert("不是分组，无法移动");
      }else if(e.dropNode.name =='header-2' && e.dragNode.name.indexOf('header') != -1) {
          e.cancel = true;
          mini.alert("分组层数超过限制");
      }
  }else {//移动

  }

})